package com.ribbon.ribbonClient;

import com.google.common.collect.Lists;
import com.netflix.client.ClientException;
import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.loadbalancer.BaseLoadBalancer;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.PingUrl;
import com.netflix.loadbalancer.Server;
import com.netflix.niws.client.http.RestClient;
import com.ribbon.ribbonClient.ping.MyPing;
import com.ribbon.ribbonClient.rule.MyRule;

import java.util.List;
import java.util.stream.IntStream;

/**
 *
 */
public class Test1 {

    public static void main(String []args) throws Exception {
//        testDemo();
//        testILoadBalancer();
//        testRule();
        testPing();
    }

    //初次接触
    public static void testDemo() throws Exception {
        //直接对两个项目进行负载均衡
        ConfigurationManager.getConfigInstance().setProperty("myClient.ribbon.listOfServers", "localhost:8081,localhost:8082");
       //使用自定义规则
//      ConfigurationManager.getConfigInstance().setProperty("myClient.ribbon.NFLoadBalancerRuleClassName",MyRule.class.getName());
        //设置IPing实现类
//        ConfigurationManager.getConfigInstance().setProperty("my-client.ribbon.NFLoadBalancerPingClassName",PingUrl.class.getName());
        //设置每隔两秒ping一次
//        ConfigurationManager.getConfigInstance().setProperty("myClient.ribbon.NFLoadBalancerPingInterval",2);
        RestClient client = (RestClient) ClientFactory.getNamedClient("myClient");
        HttpRequest request = HttpRequest.newBuilder().uri("/demo/person/1").build();
        StringBuilder builder = new StringBuilder();
        for(int i = 0;i< 6;i++){
            //进行请求，查看是否成功
            HttpResponse response = client.executeWithLoadBalancer(request);
            String result = response.getEntity(String.class);
            builder.append(result + "\n");
        }
        System.out.println(builder.toString());
    }


    //负载均衡器
    public static void testILoadBalancer(){
        //负载均衡器
        ILoadBalancer lb = new BaseLoadBalancer();
        Server s1 = new Server("localhost", 8081);
        Server s2 = new Server("localhost", 8082);
        //服务器列表
        List<Server> serverList = Lists.newArrayList( s1,s2);
        //添加到负载均衡器
        lb.addServers(serverList);
        IntStream.range(0,6).forEach(i->{
            //选取某个服务器处理
            Server s = lb.chooseServer(null);
            System.out.println(s);
        });
    }

    //自定义负载规则
    public static void testRule(){
        //负载均衡器
        BaseLoadBalancer lb = new BaseLoadBalancer();
        //使用自定义的负载规则
        lb.setRule(new MyRule(lb));
        Server s1 = new Server("localhost", 8081);
        Server s2 = new Server("localhost", 8082);
        //服务器列表
        List<Server> serverList = Lists.newArrayList( s1,s2);
        //添加到负载均衡器
        lb.addServers(serverList);
        IntStream.range(0,6).forEach(i->{
            //根据自定义规则选择端口为8082的服务器
            Server s = lb.chooseServer(8082);
            System.out.println(s);
        });
    }


    //自定义Ping
    public static void testPing(){
        //负载均衡器
        BaseLoadBalancer lb = new BaseLoadBalancer();
        Server s1 = new Server("localhost", 8081);
        Server s2 = new Server("localhost", 8082);
        //服务器列表
        List<Server> serverList = Lists.newArrayList( s1,s2);
        //添加到负载均衡器
        lb.addServers(serverList);
        //设置IPing实现类
//        lb.setPing(new PingUrl());
        //自定义Ping
        lb.setPing(new MyPing());
        //每两秒ping一次
        lb.setPingInterval(2);
        IntStream.range(0,6).forEach(i->{
            //选取某个服务器处理
            Server s = lb.chooseServer(null);
            System.out.println(s+" ping状态: " +s.isAlive());
        });
    }





}
